package com.zf.shuashua.advert;

import android.app.Activity;

import androidx.annotation.NonNull;

import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.AdvertUtils;
import com.zf.shuashua.advert.mg.AdHelperReward;
import com.zf.shuashua.advert.mg.RewardListener;
import com.zf.shuashua.bean.AdvertReport;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.manager.ActionThirdReportManager;
import com.zf.shuashua.utils.ToastUtil;
import com.zf.shuashua.utils.Util;

import java.lang.ref.WeakReference;

/**
 * 激励视频展示
 * 芒果激励视频
 */
public class RewardAdvertMgManager {

    private AdHelperReward adHelperReward;
    private String mAdvertType = AdvertConstants.REWARD_TYPE_TASK;
    private String mCodeId = "10050";
    private String mReportID = "";

    private static RewardAdvertMgManager instance;
    private WeakReference<Activity> weakReference;
    private boolean isComplete = false;

    private RewardAdvertMgManager() {
    }

    public static RewardAdvertMgManager getInstance() {
        if (null == instance) {
            synchronized (RewardAdvertMgManager.class) {
                if (null == instance) {
                    instance = new RewardAdvertMgManager();
                }
            }
        }
        return instance;
    }

    public void showAdvert(Activity activity, String advertType,  String reportId, OnRewardListener listener) {
        this.weakReference = new WeakReference<>(activity);
        this.mAdvertType = advertType;
        this.mReportID = reportId;
        isComplete = false;

//        mCodeId = getCodeId();
        showByteAdvert(activity, listener);
    }

    private String planId;
    private String uuiThirdAdvert;
    private void showByteAdvert(Activity activity, OnRewardListener listener) {
        uuiThirdAdvert = Util.getUUID32();
        adHelperReward = new AdHelperReward(activity, "reward", null, new RewardListener() {
            @Override
            public void onAdStartRequest(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "\n开始请求: " + providerType);
            }

            @Override
            public void onAdLoaded(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "请求到了: " + providerType);
                weakReference.get().runOnUiThread(() -> showAdvert());
                reportRequestSucc(1);
            }

            @Override
            public void onAdFailed(@NonNull String providerType, String failedMsg) {
//                ToastUtil.showToast(activity, "请求失败: " + providerType + "，" + failedMsg);
                weakReference.get().runOnUiThread(() -> {
                    if (listener != null) {
                        listener.onVideoError();
                    }
                });
                reportRequestFail(providerType, failedMsg);
            }

            @Override
            public void onAdFailedAll() {
//                ToastUtil.showToast(activity, "全部失败");
//                weakReference.get().runOnUiThread(() -> {
//                    if (listener != null) {
//                        listener.onVideoError();
//                    }
//                });
                reportRequestFail("11", "全部失败");
            }

            @Override
            public void onAdClicked(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "点击了: " + providerType);
                if (listener != null) {
                    listener.onAdClick();
                }
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoClick, getReportType(), "5");
            }

            @Override
            public void onAdShow(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "展示了: " + providerType);
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType(),"5");
            }

            @Override
            public void onAdExpose(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "曝光了: " + providerType);
            }

            @Override
            public void onAdVideoComplete(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "视频播放完成: " + providerType);
                isComplete = true;
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onAdVideoCached(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "视频已缓存: " + providerType);
            }

            @Override
            public void onAdRewardVerify(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "激励验证: " + providerType);
                isComplete = true;
            }

            @Override
            public void onAdClose(@NonNull String providerType) {
//                ToastUtil.showToast(activity, "关闭了: " + providerType);
                dealClose(planId, listener);
            }
        });
        planId = System.identityHashCode(adHelperReward) + "";

        adHelperReward.load();
        reportRequestStart();
    }


    private void showAdvert() {
        adHelperReward.show();
//        reportRequestSucc(1);
    }

    private void reportRequestStart(){
        ActionThirdReportManager.getInstance().reportRequestStart(5, 2, mCodeId, 1);
    }

    private void reportRequestSucc(int return_num) {
        ActionThirdReportManager.getInstance().reportRequestSucc(5, 2, mCodeId, return_num);
    }

    private void reportRequestFail(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestFail(5, 2, mCodeId, 1, error_code, error_msg);
    }

    private void reportRequestRend(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestRend(5, 2, mCodeId, error_code, error_msg);
    }

    private void dealClose(String planId, OnRewardListener listener){
        if(!isComplete){
            return;
        }
        AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoEnd, getReportType(), "5", new ACallback<BaseData>() {
            @Override
            public void onSuccess(BaseData data) {
                if (data != null && data.isStatus()) {
                    weakReference.get().runOnUiThread(() -> {
                        if (listener != null) {
                            listener.onAdClose();
                        }
                    });
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

    private String getReportType() {
        return AdvertUtils.getReportType(mAdvertType, mReportID);
    }
}
